﻿package gui.hug;

import flash.display.Sprite;
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.events.Event;

class HUGCheckBox extends LabelButton {
	private var isChecked : Bool;
	private var isOver : Bool;
   
	public function new( label:String, x:Int, y:Int ) {
		super( label, 19, -2 );

		isChecked = false;
		isOver = false;

		this.x = x;
		this.y = y;

		reDraw();

		addEventListener( MouseEvent.MOUSE_OVER, onOver  );
		addEventListener( MouseEvent.MOUSE_OUT , onOut   );
		addEventListener( MouseEvent.MOUSE_DOWN, onClick );

		buttonMode = true;
		mouseChildren = false;
	}
    
    public function getChecked():Bool
    {
      return isChecked;
    }
    
    public function setChecked( b:Bool )
    {
      isChecked = b;
      reDraw();
    }
    
    // ----------------------------------------------------------------------- private methods
    private function reDraw()
    {
      graphics.clear();
      
      // Transparent BackGround
      Utils.fillRect( this, 0, 0, width, 15, 0, 0 );

      Utils.rect( this, 0, 0, 13, 13, isOver ? 0xdae1e8 : 0xdae1e8, 0xc0cad5 );
      
      if ( isChecked )
      {
        var c:UInt = isOver ? 0xb7c0cb : 0xdfe5ea;
        var i:UInt;
        
        // Back
        for (i in 2...6)
          Utils.horLine( this, Math.max(3, i), i+2, i+6, c );

        var i2:UInt = isOver ? 13 : 11;
        for (i in 6...13)
          Utils.horLine( this, i, Math.min(i2 + 1, i + 2), 16 - i, c );
          
        if ( !isOver )
          Utils.vertLine( this, 14, 3, 4, 0xdfe5ea );

        // Front
        for (i in 3...6)
			Utils.horLine( this, i, i + 2, i + 4, 0x5f83a5 );
        
        Utils.vertLine( this, 4, 6, 7, 0x5f83a5 );
        Utils.vertLine( this, 6, 8, 10, 0x5f83a5 );
          
        for (i in 6...14)
          Utils.horLine( this, i, i + ((i == 13) ? 1 : 2), 14 - i, 0x5f83a5 );
      }
    }
    
    // ----------------------------------------------------------------------- Event handlers
    private function onOver( e:MouseEvent )
    {
      isOver = true;
      reDraw();
    }

    private function onOut( e:MouseEvent )
    {
      isOver = false;
      reDraw();
    }

    private function onClick( e:MouseEvent )
    {
      isChecked = !isChecked;
      reDraw();
      dispatchEvent( new Event( Event.CHANGE ) );
    }
  }
